package org.apak.frontend;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apak.control.ErrorList;
import org.apak.control.InvoiceForm;
import org.apak.control.ServicePartForm;
import org.apak.control.Validator;
import org.apak.db.RepairServiceDAO;
import org.apak.log.FileLogger;
import org.apak.model.Customer;
import org.apak.model.Device;
import org.apak.model.Invoice;
import org.apak.model.InvoiceRow;
import org.apak.model.InvoiceStatusType;
import org.apak.model.LoggedSubject;
import org.apak.model.ServiceOrder;

/**
 * Servlet implementation class InvoiceServlet
 */
@WebServlet("/InvoiceServlet")
public class InvoiceServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private RepairServiceDAO dao = new RepairServiceDAO();
	private HttpSession session;
	private int invoiceID;
	private Validator validator = new Validator();
	String serviceOrderID;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		session = request.getSession(true);
		LoggedSubject loggedSubject = (LoggedSubject) session.getAttribute("loggedSubject");
		if(loggedSubject == null) {
			RequestDispatcher dispatcher = request.getRequestDispatcher("error.jsp");
			dispatcher.forward(request, response);
		} else {
			serviceOrderID = (String) request.getParameter("serviceOrder");
			Customer[] customerArray = dao.getCustomerList();
			request.setAttribute("customerArray", customerArray);
			if(serviceOrderID != null) {
				if(dao.getServiceOrder(serviceOrderID) == null){
					RequestDispatcher dispatcher = request.getRequestDispatcher("error.jsp");
					dispatcher.forward(request, response);
					return;
				}
				request.setAttribute("serviceOrderID", serviceOrderID);
				InvoiceStatusType[] invoiceStatusTypeArray = dao.getInvoiceStatusTypeList();
				request.setAttribute("invoiceStatusTypeArray", invoiceStatusTypeArray);
				Invoice invoice = dao.getInvoiceByServiceOrder(serviceOrderID);
				request.setAttribute("invoice", invoice);
				ServiceOrder serviceOrder = dao.getServiceOrder(serviceOrderID);
				if(serviceOrder != null && serviceOrder.getSo_status_type_fk() == 4 || serviceOrder.getSo_status_type_fk() == 5) {
					request.setAttribute("disabled", true);
					request.setAttribute("toServiceOrder", true);
				}
				if(invoice != null) {
					//System.out.println("descr " + invoice.getDescription());
					invoiceID = invoice.getInvoice();
					//dao.updateInvoiceTotalPrice(invoice.getInvoice());
					InvoiceRow[] invoiceRowArray = dao.getInvoiceRowList(invoice.getInvoice());
					//System.out.println("length " +invoiceRowArray.length );
					request.setAttribute("invoiceRowArray", invoiceRowArray);
				} else {
					invoiceID = dao.insertInvoice(serviceOrder);
					//dao.updateInvoiceTotalPrice(invoice.getInvoice());
					InvoiceRow[] invoiceRowArray = dao.getInvoiceRowList(invoiceID);
					//System.out.println("length " +invoiceRowArray.length );
					request.setAttribute("invoiceRowArray", invoiceRowArray);
					response.sendRedirect(String.format("./invoice?serviceOrder=%s",serviceOrderID));
					return;
				}
			}
			if(loggedSubject.isCustomer()) {
				request.setAttribute("disabled", true);
				request.setAttribute("toServiceOrder", false);
			}
			RequestDispatcher dispatcher = request.getRequestDispatcher("invoice.jsp");
			dispatcher.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Customer[] customerArray = dao.getCustomerList();
		request.setAttribute("customerArray", customerArray);
		InvoiceStatusType[] invoiceStatusTypeArray = dao.getInvoiceStatusTypeList();
		request.setAttribute("invoiceStatusTypeArray", invoiceStatusTypeArray);
		InvoiceRow[] invoiceRowArray = dao.getInvoiceRowList(invoiceID);
		request.setAttribute("invoiceRowArray", invoiceRowArray);
		String actionButton = request.getParameter("actionButton");
		InvoiceForm invoiceForm = new InvoiceForm(invoiceID,
				request.getParameter("receiver_name"), request.getParameter("accounts"),
				request.getParameter("reference_number"), request.getParameter("status"),
				request.getParameter("price"), request.getParameter("employeeDescription"), request.getParameter("customer"));
		if(actionButton != null)
			if(actionButton.equals("Save")) {
				request.setAttribute("serviceOrderID", serviceOrderID);
				ErrorList errors = validator.findErrors(invoiceForm);
				if(errors.isErrorFound()) {
					request.setAttribute("errors", errors);
					RequestDispatcher dispatcher = request.getRequestDispatcher("invoice.jsp");
					dispatcher.forward(request, response);
				} else {
					if(dao.updateInvoice(invoiceForm) != 0)
						request.setAttribute("success", "Saved successfully!");
					request.setAttribute("invoiceForm", invoiceForm);	
					RequestDispatcher dispatcher = request.getRequestDispatcher("invoice.jsp");
					dispatcher.forward(request, response);
				}
			}
	}

}
